package com.kili.picture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kili.picture.domain.entity.PictureConfig;
import com.kili.picture.domain.dao.PictureConfigMapper;
import com.kili.picture.domain.model.picture.config.PictureConfigCreateDTO;
import com.kili.picture.domain.model.picture.config.PictureConfigDetailDTO;
import com.kili.picture.domain.model.picture.config.PictureConfigUpdateDTO;
import com.kili.picture.service.PictureConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 图片配置 服务实现类
 * </p>
 *
 * @author Jar
 * @since 2019-04-01
 */
@Service
public class PictureConfigServiceImpl extends ServiceImpl<PictureConfigMapper, PictureConfig> implements PictureConfigService {

    private static final Logger logger = LoggerFactory.getLogger(PictureConfigServiceImpl.class);

    @Override
    public String getPrefixByModule(String module) {
        String prefix = null;
        PictureConfig query = new PictureConfig();
        query.setModule(module);
        PictureConfig pictureConfig = baseMapper.selectOne(new QueryWrapper<>(query));

        if (pictureConfig != null) {
            prefix = pictureConfig.getPrefix();
        }

        return prefix;
    }

    @Override
    public List<String> getPrefixAll() {
        return baseMapper.getPrefixAll();
    }

    @Override
    public PictureConfigDetailDTO create(PictureConfigCreateDTO create) {
        logger.info("create picture config:", create);

        PictureConfig entity = new PictureConfig();
        long time = System.currentTimeMillis() / 1000;
        entity.setModule(create.getModule());
        entity.setPrefix(create.getPrefix());
        entity.setCreateAt(time);
        entity.setModifyAt(time);
        entity.setStatus(PictureConfig.CREATE);

        int insert = baseMapper.insert(entity);

        if (insert != 1) {
            throw new RuntimeException("create picture config error");
        }

        PictureConfigDetailDTO detail = new PictureConfigDetailDTO();
        BeanUtils.copyProperties(entity, detail);

        return detail;
    }

    @Override
    public PictureConfigDetailDTO update(PictureConfigUpdateDTO update) {
        logger.info("update picture config:", update);

        Integer id = update.getId();

        PictureConfig entity = new PictureConfig();
        entity.setId(id);
        entity.setModule(update.getModule());
        entity.setPrefix(update.getPrefix());
        entity.setModifyAt(System.currentTimeMillis() / 1000);
        // TODO 状态校验
        entity.setStatus(update.getStatus());

        int i = baseMapper.updateById(entity);

        if (i != 1) {
            throw new RuntimeException("update picture config error");
        }

        PictureConfigDetailDTO detail = new PictureConfigDetailDTO();
        BeanUtils.copyProperties(baseMapper.selectById(id), detail);

        return detail;
    }

}
